package main.java.com.demo;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DownloadTool {
    public static void download(String dirStr, String urlStr) {
        File file=new File(dirStr);
        try {
            if(!file.exists()) {
                file.mkdirs();
            }
            int index=urlStr.lastIndexOf("/");
            if (index<0) {
                System.out.println("error: "+urlStr);
                return;
            }
            String name=urlStr.substring(index+1);
            URL url=new URL(urlStr);
            URLConnection connection = url.openConnection();
            InputStream inputStream=connection.getInputStream();
            FileOutputStream outputStream=new FileOutputStream(dirStr+File.separator+name);
            int length=1;
            byte[] buffer=new byte[1024*1024];
            while (length>-1) {
                length=inputStream.read(buffer);
                if (length>0) {
                    outputStream.write(buffer, 0, length);
                }
            }
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void download(String dirStr, List<String>urlStrs) {
        int i=0;
        for(String urlStr: urlStrs) {
            download(dirStr, urlStr);
            i++;
            System.out.println(String.format("%d/%d", i, urlStrs.size()));
        }
    }

    public static void downloadTxt(String path, String urlStr, String prefix, String cssQuery, String cssQuery2, int from) {
        try {
            FileOutputStream outputStream=new FileOutputStream(path);
            Elements elements=JsoupTool.getElements(urlStr, cssQuery);
            elements=filter(elements);
            List<String>hrefs=JsoupTool.getHrefs(elements);
            hrefs=filter(hrefs, prefix);
            List<String>titles=JsoupTool.getTexts(elements);
            for(int i=0; i<titles.size(); i++) {
                if(i<from) {
                    continue;
                }
                outputStream.write((titles.get(i)+"\r\n").getBytes(StandardCharsets.UTF_8));
                System.out.println(String.format("%d/%d", i, titles.size()));
                downloadContent(hrefs.get(i), cssQuery2, outputStream);
//                downloadContent(getUrl(hrefs.get(i)), cssQuery2, outputStream);
            }
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getUrl(String url) {
        int index=url.indexOf(".html");
        String s=url.substring(0, index)+"_2"+".html";
        return s;
    }

    public static void downloadContent(String url, String cssQuery, FileOutputStream outputStream) {
        int c=0;
        while(c<4) {
            c++;
            Elements elements=JsoupTool.getElements(url, cssQuery);
            if(elements==null) {
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                for(Element element: elements) {
                    String content=element.text().trim();
                    try {
                        outputStream.write((content+"\r\n").getBytes(StandardCharsets.UTF_8));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
            }
        }
    }

    public static Elements filter(Elements elements) {
        Elements list=new Elements();
        Iterator<Element>iterator=elements.iterator();
        while (iterator.hasNext()) {
            Element element=iterator.next();
            String text=element.text().trim();
            int c=count(text, elements);
            if(c>1) {
                iterator.remove();
            } else {
                list.add(element);
            }
        }
        return list;
    }

    public static int count(String title, Elements elements) {
        int c=0;
        for(Element element: elements) {
            String text=element.text().trim();
            if (text.equals(title)) {
                c++;
            }
        }
        return c;
    }

    public static List<String> filter(List<String> hrefs, String prefix) {
        List<String> list=new ArrayList<>();
        Iterator<String> iterator=hrefs.iterator();
        while (iterator.hasNext()) {
            String href=iterator.next();
            if(!href.startsWith(prefix)) {
                href=prefix+href;
            }
            list.add(href);
        }
        return list;
    }
}
